myTempIsLogged = false;


function userShowLogoutButton()

{
	if ($('.ui-tabs-nav #logoutButton').length == 0) {
		// Création du bouton et ajout au DOM.
		var myLogoutButton = $('<li>');
		var myLogoutLink = $('<a>');
		myLogoutLink.attr('id', 'logoutButton');
		myLogoutLink.attr('href', '#');
		myLogoutLink.text('Se deconnecter');
		myLogoutButton.append(myLogoutLink);
		myLogoutButton.attr('class', 'ui-state-default ui-corner-top');
		$('.ui-tabs-nav').append(myLogoutButton).fadeIn();
		$('.ui-tabs-nav #logoutButton').click(function(myEvent) {
			myEvent.preventDefault();
			myRouter.setRoute('user/logout');
			$(this).remove();
			return false;
		});
	}
}


function userCookieSettings() 

{
	return {
		expires: 7, 
		path: '/' 
	};
}


function userIsLogged() 

{
	//return ($.cookie('sessionId') != null);
	return myTempIsLogged;
}


function userLogout() 

{
	/*if (userIsLogged()) {
		$.cookie('sessionId', null);
		$.cookie('sessionUsername', null);
	}*/
	myTempIsLogged = false;
}


function userAuthRequired()

{
	if (!userIsLogged())
		myRouter.setRoute('user/login');
}


function userAuthForbidden()

{
	if (userIsLogged())
		myRouter.setRoute('home');
}


function userLogin(myUserId, myUserName) 

{
	/*if (!userIsLogged()) {
		$.cookie('sessionId', myUserId, userCookieSettings());
		$.cookie('sessionUsername', myUserName, userCookieSettings());
		return true;
	}
	return false;*/
	userShowLogoutButton();
	myTempIsLogged = true;
	return true;
}


$(document).ready(function () {
	$('#loginFormErrors').hide();
	$('#registrationFormErrors').hide();
	$('#loginForm').submit(function(myEvent) {
		myEvent.preventDefault();
		$('#loginFormErrors').hide();
		$('#loginFormErrorList').children().remove();
		$.ajax({
			url: "tmp/userLoggedIn.json",
			type: 'GET',
			dataType: 'json', 
			data: {
				'userName': $('#loginUserName').text(),
				'password': $('#loginUserPassword').text()
			},
			success: function(myData) {
				if (userLogin(myData.username, myData.password)) {
					myRouter.setRoute('home');
				} else {
					$('#loginFormErrorList').append('<li>Invalid login or password.</li>');
					$('#loginFormErrors').fadeIn();
				}
			},
			error: function(jqXHR, textStatus, errorThrown) {
				$('#loginFormErrorList').append('<li>Impossible de se connecter au serveur.</li>');
				$('#loginFormErrors').fadeIn();
			}
		});
		return false;
	});
	$('#registrationForm').submit(function(myEvent) {
		myEvent.preventDefault();
		$('#registrationFormErrors').hide();
		$('#registrationFormErrorList').children().remove();
		$.ajax({
			url: "tmp/userRegistration.json",
			type: 'GET',
			dataType: 'json', 
			data: {
				'userName': $('#regUserName').text(),
				'password': $('#regUserPassword').text()
			},
			success: function(myData) {
				if (userLogin(myData.username, myData.password)) {
					myRouter.setRoute('home');
				} else {
					$('#registrationFormErrorList').append('<li>Invalid login or password.</li>');
					$('#registrationFormErrors').fadeIn();
				}
			},
			error: function(jqXHR, textStatus, errorThrown) {
				$('#registrationFormErrorList').append('<li>Impossible de se connecter au serveur.</li>');
				$('#registrationFormErrors').fadeIn();
			}
		});
		return false;
	});
	$('a#loginGoToRegistration').click(function(myEvent) {
		myEvent.preventDefault();
		$('#registrationFormErrors').hide();
		myRouter.setRoute('user/register');
		return false;
	});
	$('a#regGoToLogin').click(function(myEvent) {
		myEvent.preventDefault();
		$('#loginFormErrors').hide();
		myRouter.setRoute('user/login');
		return false;
	});
	$('a#logoutGoToLogin').click(function(myEvent) {
		myEvent.preventDefault();
		$('#loginFormErrors').hide();
		myRouter.setRoute('user/login');
		return false;
	});
});